home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / btwalker.cpp < prev    next >
C/C++ Source or Header  |  1999-03-14  |  9KB  |  304 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: btwalker.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 12/02/1998 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The BtreeWalkerb class defines a generic iterator used to walk
  32. through balanced multi-way file-base trees. The BtreeWalker
  33. class is a general-purpose class used to sort the entry keys
  34. and perform search operations.
  35. */
  36. // ----------------------------------------------------------- // 
  37. #include "btwalker.h"
  38. #include "strutil.h"
  39.  
  40. BtreeWalkerb::BtreeWalkerb(Btree *t, BtreeWalkOrder w)
  41.   : root(*t->GetCache()), curr(*t->GetCache())
  42. {
  43.   Reset(t, w);
  44. }
  45.  
  46. void BtreeWalkerb::Reset(Btree *t, BtreeWalkOrder w) 
  47. {
  48.   // Ensure that the in memory buffers and the file data
  49.   // stay in sync during multiple file access.
  50.   t->TestTree();
  51.  
  52.   tree = t;
  53.   root = t->GetRoot();
  54.   worder = w;
  55.   if (worder == btPREORDER) NextFptr = &BtreeWalkerb::NextPreOrder;
  56.   else if (worder == btINORDER) NextFptr = &BtreeWalkerb::NextInOrder;
  57.   else if (worder == btPOSTORDER) NextFptr = &BtreeWalkerb::NextPostOrder;
  58.   else NextFptr = &BtreeWalkerb::NextLvlOrder;
  59.   state = 0;
  60.   curr = root;
  61.   path.Clear();
  62.   right_path.Clear();
  63.   if ((__LWORD__)root && worder == btLVLORDER) path.Insert((__LWORD__)root);
  64. }
  65.  
  66. CachePointer BtreeWalkerb::NextPreOrder()
  67. // Visit root first, then left, then right subtree 
  68. {
  69.   __LWORD__ addr;       // Current address
  70.   CachePointer p(curr); // Parent node pointer
  71.  
  72.   while(1) {
  73.     if((__LWORD__)curr) { 
  74.       path.Push((__LWORD__)curr);
  75.       p = curr;
  76.       curr = curr->left;
  77.       return p;
  78.     }
  79.     else if(right_path.Extract(addr) != 0) {
  80.       curr = addr;  
  81.     }
  82.     else {
  83.       if(path.Pop(addr) == 0) {
  84.     curr = 0;
  85.     return curr; // No more nodes in the tree
  86.       }
  87.       else {
  88.     p = addr;
  89.     int n = p->cnt;
  90.     for(int i = 0; i < n; i++) {
  91.       if(p->entry[i].right != 0) {
  92.         right_path.Insert((__LWORD__)p->entry[i].right);
  93.       }
  94.     }
  95.     curr = 0; // Travel the right path during the next iteration
  96.       }
  97.     }
  98.   }
  99. }
  100.  
  101. CachePointer BtreeWalkerb::NextInOrder()
  102. // Visit left subtree first, then root, then right 
  103. {
  104.   __LWORD__ addr;       // Current address
  105.   CachePointer p(curr); // Parent node pointer
  106.   
  107.   while(1) {
  108.     if((__LWORD__)curr) { 
  109.       path.Push((__LWORD__)curr);
  110.       curr = curr->left;
  111.     }
  112.     else if(right_path.Pop(addr) != 0) {
  113.       curr = addr;  
  114.     }
  115.     else {
  116.       if(path.Pop(addr) == 0) {
  117.     curr = 0;
  118.     return curr; // No more nodes in the tree
  119.       }
  120.       else {
  121.     p = addr;
  122.     int n = p->cnt;
  123.     for(int i = 0; i < n; i++) {
  124.       if(p->entry[i].right != 0) {
  125.         right_path.Push((__LWORD__)p->entry[i].right);
  126.       }
  127.     }
  128.     curr = 0; // Travel the right path during the next iteration
  129.     return p;
  130.       }
  131.     }
  132.   }
  133. }
  134.  
  135. CachePointer BtreeWalkerb::NextLvlOrder()
  136. // Visit level by level, start at root, and go left to right
  137. {
  138.   __LWORD__ addr = 0;
  139.   CachePointer p(curr); p = 0;
  140.   if(path.Extract(addr) == 0) return p; else curr = addr;
  141.   int n = curr->cnt;
  142.   if(curr->left != 0) path.Insert((__LWORD__)curr->left);
  143.   for(int i = 0; i < n; i++) {
  144.     if(curr->entry[i].right != 0) path.Insert((__LWORD__)curr->entry[i].right);
  145.   }
  146.   return curr;
  147. }
  148.  
  149. CachePointer BtreeWalkerb::NextPostOrder()
  150. // Visit Left subtree first, then Right, then Root 
  151. {
  152.   __LWORD__ addr, right_addr = 0;
  153.   CachePointer c(curr); 
  154.   
  155.   while(1) {
  156.     if(state == 0) { // Ready to go down the tree to left
  157.       if((__LWORD__)curr) { 
  158.     path.Push((__LWORD__)curr);
  159.     for(int i = 0; i < curr->cnt; i++) {
  160.       if(curr->entry[i].right != 0) {
  161.         right_path.Push((__LWORD__)curr->entry[i].right);
  162.       }
  163.     }
  164.     curr = curr->left;
  165.       }
  166.       else state = 1;
  167.     }
  168.     else { // State == 1: // Ready to come up the tree
  169.       c = curr;
  170.       if(path.IsEmpty()) {
  171.     curr = 0;
  172.     return curr; // At root
  173.       }
  174.  
  175.       addr = *path.Top();
  176.       curr = addr;
  177.  
  178.       if((__LWORD__)c == curr->left && right_path.Pop(right_addr) != 0) {
  179.     // Coming back up the tree from the left, and
  180.     // there is a right child, so go right. 
  181.     // Note that curr is still on top of stack.
  182.     curr = right_addr;
  183.     state = 0;
  184.       }
  185.       else {
  186.           // Coming back up the tree from the right,
  187.           // or there was no right child, so visit
  188.           // the node, and continue on up. (State
  189.           // stays at 1.)
  190.     path.Pop();
  191.     return curr;
  192.       }
  193.     }
  194.   }
  195. }
  196.  
  197. int BtreeWalker::Find(const EntryKey &key, EntryKey &e)
  198. // Finds a specified entry key. Will return true if the key is found
  199. // or false if the key is not found. The key name, object address,
  200. // and class ID of the matching entry key will be passed back in
  201. // entry key "e". 
  202. {
  203.   CachePointer nxt(*btx->GetCache());
  204.   BtreeWalkerb tw(btx, btINORDER);
  205.   nxt = btx->GetRoot();
  206.  
  207.   while((__LWORD__)nxt != 0) {
  208.     nxt = tw.Next();
  209.     if((__LWORD__)nxt) {
  210.       int n = nxt->cnt;
  211.       for(int i = 0; i < n; i++) {
  212.     if(Compare(nxt->entry[i], key) == 0) {
  213.       e = nxt->entry[i];
  214.       return 1;
  215.     }
  216.       }
  217.     }
  218.   }
  219.   return 0;
  220. }
  221.  
  222. unsigned BtreeWalker::Sort(EntryKeyVisitFunc Visit)
  223. // Sorts the entry keys in alphabetical order. The visit action
  224. // defines a procedure used to process entry keys as they are sorted.
  225. {
  226.   CachePointer nxt(*btx->GetCache());
  227.   BtreeWalkerb tw(btx, btINORDER);
  228.   int i, j;
  229.   nxt = btx->GetRoot();
  230.   DLList<EntryKey> list; // Short temporary list used to sort keys 
  231.   DNode<EntryKey> *ptr;  
  232.   DNode<EntryKey> *next_ptr;
  233.   
  234.   unsigned num_objects = 0;
  235.   
  236.   while((__LWORD__)nxt != 0) {
  237.     nxt = tw.Next();
  238.     if((__LWORD__)nxt) {
  239.       if(nxt->left != 0) {
  240.     for(i = 0; i < nxt->cnt; i++) {
  241.       list.StoreNode(nxt->entry[i]);
  242.     }
  243.     continue; 
  244.       }
  245.  
  246.       if(!list.IsEmpty()) {
  247.     ptr = list.GetFront();
  248.     for(i = 0, j = 0; i < nxt->cnt; i++) {
  249.       while(!list.IsHeader(ptr)) {
  250.         next_ptr = ptr->GetNext();
  251.         if(FullCompare(ptr->Data, nxt->entry[i].key) < 0) {
  252.           (*Visit)(ptr->Data);
  253.           num_objects++;
  254.           list.Delete(ptr);
  255.         }
  256.         ptr = next_ptr;
  257.       }
  258.     }
  259.     for(; j < nxt->cnt; j++) {
  260.       (*Visit)(nxt->entry[j]);
  261.       num_objects++;
  262.     }
  263.       }
  264.       else {
  265.     for(i = 0; i < nxt->cnt; i++) {
  266.       (*Visit)(nxt->entry[i]);
  267.       num_objects++;
  268.     }
  269.       }
  270.     }
  271.   }
  272.   return num_objects;
  273. }
  274.  
  275. unsigned BtreeWalker::Partiallookup(const char *p, EntryKeyVisitFunc Visit)
  276. // Searches for sub-string pattern "p" in each entry key. The visit action
  277. // defines a procedure used to process the entry key if a matching sub-string
  278. // is found. Returns the number of matches found or zero if no matching
  279. // sub-strings are found. 
  280. {
  281.   CachePointer nxt(*btx->GetCache());
  282.   BtreeWalkerb tw(btx, btINORDER);
  283.   int i, rv, matches = 0;
  284.   nxt = btx->GetRoot();
  285.  
  286.   while((__LWORD__)nx